package com.chen.springboot.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.springboot.entity.Address;
import com.chen.springboot.exception.ServiceException;
import com.chen.springboot.mapper.AddressMapper;
import com.chen.springboot.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author Chen
 * @Date 2024/12/18 20:42
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public Page<Address> getPage(Integer pageNum, Integer pageSize, Integer userId) {
        Page<Address> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Address> wrapper = Wrappers.lambdaQuery();
        // 查询当前的 userId
        wrapper.eq(Address::getUserId, userId);
        return addressMapper.selectPage(page, wrapper);
    }

    @Override
    public void add(Address address) {
        if (addressMapper.insert(address) == 0)
            throw new ServiceException("400", "新增地址插入失败");
    }

    @Override
    public void deleteByPath(Integer id) {
        if (addressMapper.deleteById(id) == 0)
            throw new ServiceException("400", "删除地址失败");

    }

    @Override
    public void deleteAll() {
        if (addressMapper.delete(null) == 0)
            throw new ServiceException("400", "删除地址失败");
    }

    @Override
    public void update(Address address) {
        if (addressMapper.updateById(address) == 0)
            throw new ServiceException("400", "更新地址失败");
    }

    @Override
    public void updateById(Integer id,Integer userId) {
        addressMapper.update0(userId);
        if (!addressMapper.update1(id))
            throw new ServiceException("400", "更改状态失败");
    }

    @Override
    public String getOne(Integer userId) {
        return addressMapper.getOne(userId);
    }
}
